package com.erp.order.purchasing.dao.impl;

import com.erp.hr.base.dao.impl.BaseDaoImpl;
import com.erp.hr.base.util.CommonUtils;
import com.erp.order.purchasing.dao.InquiryListMaterialDao;
import com.erp.order.purchasing.model.InquiryListMaterial;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 询价单物资 持久实现层
 * @Author: syz
 * @blame syz
 * @CreateTime: 2021/4/6
 */
@Repository
public class InquiryListMaterialDaoImpl extends BaseDaoImpl<InquiryListMaterial> implements InquiryListMaterialDao {

    /**
     * dao支持
     */
    @Autowired
    private DaoSupport daoSupport;


    /**
     * 根据id查询对象
     * @param id 对象id
     * @return 查询结果
     */
    @Override
    public InquiryListMaterial getDataObject(String id) {
        String sql =
                "SELECT " +
                    "ilm.* " +
                "FROM " +
                    "inquiry_list_material ilm " +
                "WHERE " +
                    "ilm.id = :id " +
                "AND " +
                    "ilm.is_del = :isDel";
        Map<String, Object> args = new HashMap<>();
        args.put("id", id);
        args.put("isDel", CommonUtils.IS_DEL_NO);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("ilm", InquiryListMaterial.class);
        List<InquiryListMaterial> list = daoSupport.selectDataSql(sql, entity, args);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<InquiryListMaterial> getDataObjects(InquiryListMaterial inquiryListMaterial) {
        return null;
    }

    @Override
    public List<InquiryListMaterial> getDataObjectsForDataAuth(String s, Pages pages, InquiryListMaterial inquiryListMaterial) {
        return null;
    }

    /**
     * 根据询价id获取对象集合
     * @param id 询价id
     * @return 对象集合
     */
    @Override
    public List<InquiryListMaterial> getListByInquiryListId(Long id) {
        return daoSupport.selectDataSql(
                "SELECT " +
                    "ilm.* " +
                "FROM " +
                    "inquiry_list_material ilm " +
                "WHERE " +
                    "ilm.inquiry_list_id = :id " +
                "AND " +
                    "ilm.is_del = :isDel",
                new HashMap<String, Class<?>>(){{
                    put("ilm", InquiryListMaterial.class);
                }},
                new HashMap<String, Object>(){{
                    put("id", id);
                    put("isDel", CommonUtils.IS_DEL_NO);
                }});
    }

    @Override
    public List<InquiryListMaterial> getDataObjects(int id) {

        String sql = "SELECT il.* FROM inquiry_list_material il " +
                     "WHERE il.inquiry_list_id = :id " +
                     "order by il.id";

        Map<String, Object> args = new HashMap<>();
        args.put("id", id);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("il", InquiryListMaterial.class);
        List<InquiryListMaterial> result = daoSupport.selectDataSql(sql, entity, args);

        return result;
    }
}